﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou;

namespace customer
{
    public partial class cusChange : Skin_Mac
    {
        public cusChange()
        {
            InitializeComponent();
        }
        int i, start;//i表示总行数，start表示当前位置
        int size = 10;//size表示一页显示的数量        
        string str1 = "select * from Client";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";

        private void btn_change_Click(object sender, EventArgs e)
        {

            string id = dgv_sheet.SelectedRows[0].Cells["Column1"].Value.ToString();
            string cusname = txt_name.Text;
            string sex =cmb_sex.Text;            
            string phone = txt_tel.Text;            
            string address = txt_address.Text;
            if ( string.IsNullOrEmpty(cusname) ||
                string.IsNullOrEmpty(sex) || string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(address))
            {
                MessageBoxEx.Show("信息输入不完整！");
                return;//直接返回
            }
            string sql = string.Format("update Client set ClientName='{0}',Gender='{1}',Phone='{2}',Adderss='{3}'where ClientID='{4}'",
                 cusname,sex, phone, address,id);

            if (DBHelper.ExecuteNonQuery(sql))
            {
                MessageBoxEx.Show("客户信息修改成功！");
                string strsql = "select * from Client";
                DataTable dt = DBHelper.GetDataTable(strsql);
                dgv_sheet.DataSource = dt;
            }
            else
            {
                MessageBoxEx.Show("修改失败,请联系管理员!");
            }
        }

        private void cusChange_Load(object sender, EventArgs e)
        {
            string strsql = "select * from Client";
            DataTable dt = DBHelper.GetDataTable(strsql);
            dgv_sheet.DataSource = dt;
            i = dt.Rows.Count;//获取数据的条数
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            //删除
            string select_id = dgv_sheet.SelectedRows[0].Cells["Column1"].Value.ToString();//选择的当前行第一列的值，也就是ID
            string sql = "delete from Client where Clientid='" + select_id + "'";//sql删除语句
            DialogResult dr = MessageBoxEx.Show("您确定要删除编号为" + select_id + "的信息吗？", "警告", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    if (DBHelper.ExecuteNonQuery(sql))
                    {
                        MessageBoxEx.Show("确定要删除吗？", "系统提示：");
                        string strsql = "select * from Client";
                        DataTable dt = DBHelper.GetDataTable(strsql);
                        dgv_sheet.DataSource = dt;
                        i = dt.Rows.Count;//获取数据的条数
                    }
                    else
                    {
                        MessageBoxEx.Show("失败");
                    }
                }
                catch (Exception)
                {
                    MessageBoxEx.Show("请先删除该客户的有关订单");
                }
            }
                
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            //退出
            this.Close();
        }
        private void dgv_sheet_Click(object sender, EventArgs e)
        {
            txt_name.Text = dgv_sheet.SelectedRows[0].Cells["column2"].Value.ToString();
            cmb_sex.Text = dgv_sheet.SelectedRows[0].Cells["column3"].Value.ToString();
            txt_tel.Text = dgv_sheet.SelectedRows[0].Cells["column4"].Value.ToString();
            txt_address.Text = dgv_sheet.SelectedRows[0].Cells["column5"].Value.ToString();
        }
        //首页
        private void btn_head_Click(object sender, EventArgs e)
        {
            show(0, size);//调用show函数
            start = 0;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //上一页
        private void btn_up_Click(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页.");
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //下一页
        private void btn_down_Click(object sender, EventArgs e)
        {
            if (start + 1 > i / size)
            {
                MessageBoxEx.Show("已是最后页");
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //尾页
        private void btn_end_Click(object sender, EventArgs e)
        {
            show(i / size, i - (i / size) * size);
            start = i / size;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }



        //显示i到j的数据
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[SellGoods]");//把数据库表中的i-j的记录数用来填充数据集
            dgv_sheet.DataSource = ds.Tables["[SellGoods]"];
            ds = null;//清空数据集
        }
    }
}
